import { reactive } from "vue";

const func:any = reactive({})

func.recruit = {
    type:['请选择','社招','校招','实习'],
    edu:[ '不限', '初中及以下', '中专/技校', '高中', '大专', '本科', '硕士', '博士', ],
    exp:[ '不限', '应届校招', '实习生', '1年以内', '1-3年', '3-5年', '5-10年', '10年以上' ],
    month_wages:[ '3K以下', '3-5k', '5-10k', '10-15k', '15-20k', '20-30k', '30-50k', '50k以上', '面议' ],
    year_wages:[ '','2万以下', '2-3万', '3-4万', '4-5万', '5-6万', '6-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上', '面议'],
    scale:[ '','0-20人', '20-99人', '100-499人', '500-999人', '1000-999人', '10000人以上'],
    degree:['初中及以下','中专/技校','高中','大专','本科','硕士','博士']
}
func.isGo = (is_cancel:any, end:any) => {
    let end_time = Number(end) * 1000;
    let now_time = Date.now();
    if (is_cancel == 1) {
        return false;
    } else if (end_time < now_time) {
        return false;
    }
    return true;
}

func.js_date_time = (unixtime:any,type = 0) => {
    let dateTime = new Date(parseInt(unixtime) * 1000)
    let year:any = dateTime.getFullYear();
    let month:any = dateTime.getMonth() + 1;
    let day:any = dateTime.getDate();
    let hour:any = dateTime.getHours();
    let minute:any = dateTime.getMinutes();
    // let second = dateTime.getSeconds();
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    hour = hour < 10 ? '0' + hour : hour
    minute = minute < 10 ? '0' + minute : minute
    if(type == 1){
        return year + '/' + month + '/' + day + ' ' + hour + ':' + minute;
    }
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
}

func.getState = (is_cancel:number, start:number, end:number) => {
    if(is_cancel == 1){
        return "<span class='status cancel-color'>·</span><span class='status-text'>已取消</span>";
    }

    let start_time = Number(start) * 1000;
    let end_time = Number(end) * 1000;
    let now_time = Date.now();
    if (start_time > now_time) {
        return "<span class='status preparation-color'>·</span><span class='status-text'>未开始</span>";
    } else if (end_time < now_time) {
        return "<span class='status end-color'>·</span><span class='status-text'>已结束</span>";
    } else {
        return "<span class='status ing-color'>·</span><span class='status-text'>进行中</span>";
    }
}

func.getIsState = (is_cancel:number, start:number, end:number) => {
    if(is_cancel == 1){
        return "已取消";
    }

    let start_time = Number(start) * 1000;
    let end_time = Number(end) * 1000;
    let now_time = Date.now();
    if (start_time > now_time) {
        return "未开始";
    } else if (end_time < now_time) {
        return "已结束";
    } else {
        return "进行中";
    }
}

func.restDays = (start_time:number,end_time:number) => {
    let data:any = new Date()
    let now:number = Date.parse(data) / 1000;
    let time:number = end_time - now;
    if (time < 0) {
        return {
            day: 0,
            progress: 100,
        };
    } else {
        let s:number = 24 * 60 * 60
        let temp = time / s;
        return {
            day: temp,
            progress: (time / (end_time - start_time)) * 100,
        };
    }
}


func.overDays = (is_cancel:number, start:number, end:number) => {
    if (is_cancel == 1) {
        return 0;
    } else {
        let start_time = Number(start) * 1000;
        let end_time = Number(end) * 1000;
        let now_time = Date.now();
        if (start_time > now_time) {
        return Math.ceil((end_time - now_time) / 1000 / 60 / 60 / 24);
        } else if (end_time < now_time) {
        return 0;
        } else {
        return Math.ceil((end_time - now_time) / 1000 / 60 / 60 / 24);
        }
    }
}

func.getDuration = (duration:number) => {
    if (duration) {
        let h:number = parseInt(String(duration / 60 / 60));
        let m:number = parseInt(String((duration % (60 * 60)) / 60));
        h = h < 10 ? 0 + h : h;
        m = m < 10 ? 0 + m : m;

        return h + "小时" + m + "分钟";
    } else {
        return "--";
    }
}

func.getDurationH = (duration:number) => {
    if (duration) {
        let h:number = parseInt(String(duration / 60 / 60));
        h = h < 10 ? 0 + h : h;
        return h + "小时";
    } else {
        return "--";
    }
}

func.setProgress = (complete_num:number,partake_num:number) => {
    if (partake_num < 1 || complete_num < 1) {
        return 0;
    } else {
        return parseInt(((complete_num / partake_num) * 100).toFixed(0));
    }
}

func.getGroup = (data:any) => {
    let userArr:any = []
    userArr = data.reduce((groups:any, person:any) => {
        const key = person.department;
        if (!groups[key]) {
            groups[key] = [];
        }
        groups[key].push({id:person.id,name:person.name});
        return groups;
    }, {});
    return userArr
}

func.getIsExpire = (data:any) => {
    const date = new Date();
    const timestamp = date.getTime();
    data = new Date(data);
    data = data.getTime();
    if(data < timestamp){
        return false
    }
    return true
}

func.numberToChinese = (amount:any) => {
    // 验证输入是否为合法数字
    if (isNaN(amount) || amount === '') {
        return '零元整';
    }
    
    // 转换为正数处理（忽略负号）
    amount = Math.abs(parseFloat(amount)).toFixed(2);
    
    // 分离整数和小数部分
    const parts = amount.split('.');
    const integerPart = parts[0];
    const decimalPart = parts[1];
    
    // 中文数字字符映射
    const chineseDigits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    const unitNames = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万'];
    
    // 处理整数部分
    let chineseInteger = '';
    const length = integerPart.length;
    
    for (let i = 0; i < length; i++) {
        const digit = parseInt(integerPart[i]);
        const position = length - i - 1;
        
        if (digit !== 0) {
            chineseInteger += chineseDigits[digit] + unitNames[position];
        } else {
            // 处理零的情况
            if (position === 4 || position === 8) {
                // 万位和亿位的零需要保留单位
                chineseInteger += unitNames[position];
            }
            
            // 避免连续多个零
            if (i < length - 1 && parseInt(integerPart[i + 1]) !== 0) {
                chineseInteger += chineseDigits[digit];
            }
        }
    }
    
    // 处理小数部分（角分）
    let chineseDecimal = '';
    const jiao = parseInt(decimalPart[0]);
    const fen = parseInt(decimalPart[1]);
    
    if (jiao !== 0) {
        chineseDecimal += chineseDigits[jiao] + '角';
    }
    
    if (fen !== 0) {
        chineseDecimal += chineseDigits[fen] + '分';
    }
    
    // 组合结果
    let result = '';
    
    if (chineseInteger === '') {
        result = '零元';
    } else {
        result = chineseInteger + '元';
    }
    
    if (chineseDecimal === '') {
        result += '整';
    } else {
        result += chineseDecimal;
    }
    
    return result;
}

func.ethnicGroups = () => {
    return [
        "汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族",
        "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族", "哈尼族", "哈萨克族", "傣族",
        "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族",
        "景颇族", "柯尔克孜族", "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族",
        "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族",
        "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族",
        "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族"
    ];
}

func.onImgStyle = (html:string) => {
    let newContent =  html.replace(/<img[^>]*>/gi, (match) => {
    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')
    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '')
    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '')
    match = match.replace(/style\s*=\s*(["'])(?:(?!\1).)*\1/gi, '') // 去除 style=""这样的属性
        return match
    })
    // img标签添加style属性：max-width:100%;height:auto
    return newContent.replace(/\<img/gi,
        '<img style="width:100%;height:auto;"'
    )
}

export default func
